<template>
        <el-select :clearable="clearable" :style="'width:'+width+'px'" v-model="companyCode" placeholder="请选择">
          <el-option v-for="(item,index) in list"  :key="index"  :label="item[labelKey]" :value="item[valueKey]"></el-option>
        </el-select>
</template>
<script>
import system from '@/api/system'
export default {
  model: {
    prop: 'code',
    event: 'select'
  },
  props: {
    code: String,
    clearable: {type: Boolean, default: false},
    width: {type: Number, default: 280},
    labelKey: {type: String, default: 'companyName'},
    valueKey: {type: String, default: 'companyCode'}
  },
  watch: {
    companyCode (val) {
      this.$emit('select', val || null)
    },
    code: {
      handler (val) {
        this.companyCode = val
      },
      immediate: true
    }
  },
  components: {},
  data () {
    return {
      list: [],
      companyCode: ''
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    getList () {
      system.getCompanyInfoListAPI({}).then(res => {
        this.list = res.data.list
        if (!this.code || this.code === null) {
          this.companyCode = this.list[0][this.valueKey]
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
